<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">
    <el-form-item label="司机姓名" prop="driverName">
      <el-input v-model="dataForm.driverName" placeholder="司机姓名"></el-input>
    </el-form-item>
    <el-form-item label="司机手机号" prop="driverPhone">
      <el-input v-model="dataForm.driverPhone" placeholder="司机手机号"></el-input>
    </el-form-item>
    <el-form-item label="司机驾龄" prop="driverNum">
      <el-input-number v-model="dataForm.driverNum" placeholder="司机驾龄"></el-input-number>
    </el-form-item>
    <el-form-item label="身体是否健康" prop="isHealth" >
      <el-radio v-model="dataForm.isHealth" value="1" :label="1">是</el-radio>
      <el-radio v-model="dataForm.isHealth" value="2"  :label="2">不是</el-radio>
    </el-form-item>
    <el-form-item label="违章记录" prop="isRegulations">
      <el-radio v-model="dataForm.isRegulations" value="1" :label="1">有</el-radio>
      <el-radio v-model="dataForm.isRegulations" value="2" :label="2">无</el-radio>
    </el-form-item>
    <el-form-item label="重大交通事故" prop="isAccident">
      <el-radio v-model="dataForm.isAccident" value="1" :label="1">有</el-radio>
      <el-radio v-model="dataForm.isAccident" value="2" :label="2">无</el-radio>
    </el-form-item>
    <el-form-item label="司机照片" prop="driverImage">
      <el-upload
        v-model="dataForm.driverImage"
        class="avatar-uploader"
        :action="url"
        :show-file-list="false"
        :on-success="successHandle"
        :before-upload="beforeUploadHandle">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isMobile } from '@/utils/validate'
  export default {
    data () {
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        url: '',
        imageUrl: '',
        visible: false,
        dataForm: {
          id: 0,
          entId: '',
          driverName: '',
          driverPhone: '',
          driverNum: '',
          isHealth: '',
          isRegulations: '',
          isAccident: '',
          driverImage: ''
        },
        dataRule: {
          driverName: [
            { required: true, message: '司机姓名不能为空', trigger: 'blur' }
          ],
          driverPhone: [
            { required: true, message: '司机手机号不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ],
          driverNum: [
            { required: true, message: '司机驾龄不能为空', trigger: 'blur' }
          ],
          isHealth: [
            { required: true, message: '身体是否健康不能为空', trigger: 'blur' }
          ],
          isRegulations: [
            { required: true, message: '违章记录不能为空', trigger: 'blur' }
          ],
          isAccident: [
            { required: true, message: '重大交通事故不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created () {
      this.url = this.$http.adornUrl(`/sys/minio/upload?token=${this.$cookie.get('Ent-token')}`)
    },
    methods: {
      successHandle (response, res, file) {
        this.imageUrl = window.URL.createObjectURL(res.raw)
        if (response && response.code === 0) {
          if (this.num === this.successNum) {
            this.dataForm.driverImage = response.url
            this.$message.info('上传成功')
          }
        } else {
          this.$message.error(response.msg)
        }
      },
      beforeUploadHandle (file) {
        const isLt2M = file.size / 1024 / 1024 < 2
        if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
          this.$message.error('只支持jpg、png、gif格式的图片！')
          return false
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
          return false
        }
      },
      init (id) {
        this.dataForm.id = id || 0
        this.imageUrl = ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/app/entdriver/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              debugger
              if (data && data.code === 0) {
                this.dataForm.entId = data.entDriver.entId
                this.dataForm.driverName = data.entDriver.driverName
                this.dataForm.driverPhone = data.entDriver.driverPhone
                this.dataForm.driverNum = data.entDriver.driverNum
                this.dataForm.isHealth = data.entDriver.isHealth
                this.dataForm.isRegulations = data.entDriver.isRegulations
                this.dataForm.isAccident = data.entDriver.isAccident
                this.dataForm.driverImage = data.entDriver.driverImage
                this.imageUrl = data.entDriver.driverImage
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/entdriver/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'entId': this.dataForm.entId,
                'driverName': this.dataForm.driverName,
                'driverPhone': this.dataForm.driverPhone,
                'driverNum': this.dataForm.driverNum,
                'isHealth': this.dataForm.isHealth,
                'isRegulations': this.dataForm.isRegulations,
                'isAccident': this.dataForm.isAccident,
                'driverImage': this.dataForm.driverImage
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
